package com.ad.pet.app.login.fragment;

import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.ad.pet.App;
import com.ad.pet.R;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.app.login.LoginComponet;
import com.ad.pet.common.data.utils.SpUtil;
import com.ad.pet.common.data.utils.ui.ToastUtil;
import com.ad.pet.common.structure.events.Events;
import com.ad.pet.common.structure.events.RxBus;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.fragment.BaseFragment;
import com.ad.pet.constant.Constant;
import com.ad.pet.models.remote.User;
import com.ad.pet.ui.InputView;
import com.jakewharton.rxbinding.widget.RxTextView;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.functions.Action1;

/**
 * Created by SEELE on 2018/10/24.
 */

public class EmailRegistFragment extends BaseFragment implements AccountLoginContract.View {
    @BindView(R.id.input_account)
    InputView mInputAccount;
    @BindView(R.id.iv_pic_code)
    ImageView mIvPicCode;
    @BindView(R.id.input_phone_code)
    InputView mInputCode;
    @BindView(R.id.input_pwd)
    InputView mInputPwd;
    @BindView(R.id.input_invate_code)
    InputView mInputInvate;
    private boolean isEye;
    //    @BindView(R.id.input_pic_code)
//    InputView mInputConfirmPwd;
//    @BindView(R.id.cb_agree)
//    CheckBox mCbAgree;
//    @BindView(R.id.tv_agree)
//    TextView mTvAgree;
//    @BindView(R.id.linear_agree)
//    LinearLayout mLinearAgree;
    @BindView(R.id.btn_commit)
    Button mBtnCommit;

//    @BindView(R.id.cb_girl)
//    CheckBox mCbGirl;
//    @BindView(R.id.cb_boy)
//    CheckBox mCbBoy;

    @Inject
    RxDisposable mRxDisposable;
//    boolean isAccount;
//    boolean isPwd;
    //    boolean isImage;
    @Inject
    AccountLoginPresenter mAccountPresenter;


    private String mThirdType;
    private String mThirdValue;
    private String mThirdName;

    boolean isCanCode = true;
//    @BindView(R.id.tv_private)
//    TextView mTvPrivate;


//    CheckBox[] mCheckSexs;

    @Override
    protected void initData() {
    }

    @Override
    protected void initEvents() {
        mIvPicCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isEye = !isEye;
                setEye();
            }
        });

        mInputCode.getTvRight().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(mInputAccount.getContent())) {
                    ToastUtil.getToastUtil().showShort(mInputAccount.getEtContent().getHint().toString());
                    return;
                }
                if (isCanCode) {
                    isCanCode = false;
                    mAccountPresenter.getEmailCode(mInputAccount.getContent(), "1", "60", "2");
                }
            }
        });
        mBtnCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (TextUtils.isEmpty(mInputAccount.getContent())) {
                    ToastUtil.getToastUtil().showShort(mInputAccount.getEtContent().getHint().toString());
                    return;
                }

                if (TextUtils.isEmpty(mInputCode.getContent())) {
                    ToastUtil.getToastUtil().showShort(mInputCode.getEtContent().getHint().toString());
                    return;
                }

                if (TextUtils.isEmpty(mInputPwd.getContent())) {
                    ToastUtil.getToastUtil().showShort(mInputPwd.getEtContent().getHint().toString());
                    return;
                }
                if (mInputPwd.getContent().length() < 6 || mInputPwd.getContent().length() > 16) {
                    ToastUtil.getToastUtil().showShort("密码请输入6-16个字符");
                    return;
                }

                mAccountPresenter.register(mInputAccount.getContent(), mInputCode.getContent(), mInputPwd.getContent(),mInputInvate.getContent());

            }
        });

//        mRxDisposable.add(RxTextView.textChanges(mInputAccount.getEtContent()).subscribe(new Action1<CharSequence>() {
//            @Override
//            public void call(CharSequence charSequence) {
//                isAccount = charSequence.length() > 0;
//                mBtnCommit.setEnabled(isAccount & isPwd );
//            }
//        }));
//        mRxDisposable.add(RxTextView.textChanges(mInputPwd.getEtContent()).subscribe(new Action1<CharSequence>() {
//            @Override
//            public void call(CharSequence charSequence) {
//                isPwd = charSequence.length() > 0;
//                mBtnCommit.setEnabled(isAccount & isPwd );
//            }
//        }));
//        mRxDisposable.add(RxTextView.textChanges(mInputConfirmPwd.getEtContent()).subscribe(new Action1<CharSequence>() {
//            @Override
//            public void call(CharSequence charSequence) {
//                isImage = charSequence.length() > 0;
//                mBtnCommit.setEnabled(isAccount & isPwd & isImage);
//            }
//        }));

//        mTvAgree.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Navigation.goCommonWebView(getActivity(), getString(R.string.daigou_use_agree), LyxUtils.muchLuanguageUrl("/about/termsconditions"));
//            }
//        });
//        mTvPrivate.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Navigation.goCommonWebView(getActivity(), getString(R.string.private_policy), LyxUtils.muchLuanguageUrl("/about/privacypolicy.html"));
//            }
//        });

    }

    @Override
    protected void initViews() {
        mAccountPresenter.onCreate(this);
        setEye();

//        mCheckSexs = new CheckBox[]{mCbBoy, mCbGirl};
//        mCbBoy.setOnCheckedChangeListener(this);
//        mCbGirl.setOnCheckedChangeListener(this);

    }

    @Override
    public void initInjector() {
        getComponent(LoginComponet.class).inject(this);
    }

    @Override
    public int initContentView() {
        return R.layout.fragment_email_regist;
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    public void onDestroy() {
        if (mRxDisposable != null)
            mRxDisposable.clear();
        super.onDestroy();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {
        ToastUtil.getToastUtil().showShort(msg);
    }


    @Override
    public void phoneLoginSuccess(User user) {
        SpUtil.getInstance().putString(Constant.ACCESS_TOKEN, user.getToken());
        SpUtil.getInstance().putString(Constant.USER_ID, user.getUser_name());
        App.username = user.getUser_name();
        App.phone = user.getUser_tel();
        ToastUtil.getToastUtil().showShort("注册成功");
        RxBus.getDefault().post(new Events.AddCarts());
        RxBus.getDefault().post("set_message_number_success");


        getActivity().finish();
    }

    @Override
    public void accountLoginSuccess(User user) {

    }


    @Override
    public void getEmailCodeSuccess() {
        ToastUtil.getToastUtil().showShort("获取验证码成功");
        mRxDisposable.add(Observable.interval(1, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
                .take(59)
                .subscribe(new Action1<Long>() {
                    @Override
                    public void call(Long aLong) {
                        isCanCode = false;
                        String format = String.format("%ds", 60 - aLong);
                        RxTextView.text(mInputCode.getTvRight()).call(format);
                    }

                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                }, new Action0() {
                    @Override
                    public void call() {
                        isCanCode = true;
                        RxTextView.text(mInputCode.getTvRight()).call(getString(R.string.get_phone_verify_code));


                    }
                }));

    }

    @Override
    public void forgetPwdResetSuccess() {

    }

    @Override
    public void resetPwdSuccesss() {

    }

    @Override
    public void registJpushSuccess() {


    }


    @Override
    public void error(int code, String msg) {
        ToastUtil.getToastUtil().showShort(msg);
        isCanCode = true;

    }


    public void setThirdType(String thirdType) {
        mThirdType = thirdType;
    }

    public void setThirdValue(String thirdValue) {
        mThirdValue = thirdValue;
    }

    public void setThirdName(String thirdName) {
        mThirdName = thirdName;
    }

//    @Override
//    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//        if (isChecked) {
//            for (int i = 0; i < mCheckSexs.length; i++) {
//                //不等于当前选中的就变成false
//                if (mCheckSexs[i].getText().toString().equals(buttonView.getText().toString())) {
//                    mCheckSexs[i].setChecked(true);
//                } else {
//                    mCheckSexs[i].setChecked(false);
//                }
//            }
//        }
//    }

    private void setEye() {

        if (isEye) {
            mIvPicCode.setImageResource(R.drawable.ic_eye);
            mInputPwd.getEtContent().setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            mIvPicCode.setImageResource(R.drawable.ic_uneye);
            mInputPwd.getEtContent().setTransformationMethod(PasswordTransformationMethod.getInstance());
        }

    }
}
